package com.linktrip.devicemanager.task;

import java.util.List;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.concurrent.Callable;
import com.linktrip.devicemanager.entity.DeviceInfo;
import com.linktrip.devicemanager.util.StringTool;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;

/**
 * @author : CaineZhu E-mail:CaineZhu@me.com
 * @version : V1.0
 * @创建时间 ： 2017年6月30日 下午5:24:14
 * @类说明 : excel 读取
 */

public class ExcelReadTask implements Callable<List<DeviceInfo>> {

	private Workbook wb;

	public ExcelReadTask(Workbook wb) {
		this.wb = wb;
	}

	@Override
	public List<DeviceInfo> call() throws Exception {
		return readWorkSheet(wb);
	}

	public List<DeviceInfo> readWorkSheet(Workbook wb) {
		// 3.得到Excel工作表对象
		List<DeviceInfo> list = new ArrayList<DeviceInfo>();
		Sheet sheet = wb.getSheetAt(0);
		// 总行数
		int trLastRow = sheet.getLastRowNum();
		for (int index = 0; index <= trLastRow; index++) {
			if (index == 0)
				continue;
			Row row = sheet.getRow(index);
			if (row != null) {
				// int tdLast = row.getLastCellNum();
				DeviceInfo info = new DeviceInfo();

				String deviceNo = getCell(row.getCell(0)).trim();
				if (!StringTool.isEmpty(deviceNo)) {
					info.setDevice_no(deviceNo);
				}

				String remark = getCell(row.getCell(1)).trim();
				if (!StringTool.isEmpty(remark)) {
					info.setRemark(remark);
				}

				String simNo = getCell(row.getCell(2)).trim();
				if (!StringTool.isEmpty(simNo)) {
					info.setSim_no(simNo);
				}

				String phoneNumber = getCell(row.getCell(3)).trim();
				if (!StringTool.isEmpty(phoneNumber)) {
					info.setPhone_number(phoneNumber);
				}

				list.add(info);

			}
		}
		return list;
	}

	@SuppressWarnings("deprecation")
	public String getCell(Cell cell) {
		// return cell.getStringCellValue();
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_NUMERIC: // 数字
			if (HSSFDateUtil.isCellDateFormatted(cell)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
				return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
			} else {
				String d = Long.toString((long) cell.getNumericCellValue());
				// System.out.println("CELL_TYPE_NUMERIC:"+d);
				return d;
			}
		case Cell.CELL_TYPE_STRING: // 字符串
			String s = cell.getStringCellValue();
			// System.out.println("CELL_TYPE_STRING:"+s);
			return s;
		case Cell.CELL_TYPE_BOOLEAN: // Boolean
			return Boolean.toString(cell.getBooleanCellValue());
		case Cell.CELL_TYPE_FORMULA: // 公式
			return "";
		case Cell.CELL_TYPE_BLANK: // 空值
			return "";
		case Cell.CELL_TYPE_ERROR: // 故障
		default:
			System.out.print("未知类型   ");
		}

		return null;
	}

	public static void main(String[] args) {
		long id = 20117060800001l;
		System.out.println(id % 100000);
	}
}
